#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdint.h>
#include <string.h>
#include "arena.h"

#if defined(__GNUC__)
//#   define memcpy __builtin_memset
#else
#   if defined(_MSC_VER)
#		include <intrin.h>
#       pragma intrinsic(memset)
#   endif
#   include <string.h>
#endif /* defined(__GNUC__) */

int main(void)
{
    arena_t* arena;
    int i, j;
    size_t size;
    void* p = NULL;
    char buf[2048];
    srand(time(NULL));
    //arena = arena_create(0);
    arena = arena_create_static(buf, 2048);
    if(arena == NULL)
    {
        printf("*** ERROR! *** [arena == NULL]\n");
        return 1;
    }
    for(i = 0; i < 1000; i++)
    {
        for(j = 0; j < 1000; j++)
        {
            size = rand() % (255) + 1;
            p = arena_malloc(arena, size);
            if(p == NULL)
            {
                printf("*** ERROR! *** [%d:%d, size = %lu]\n", j, i, size);
                return 2;
            }
            memset(p, 0xdeadbeef, size);
        }
        arena_reset(arena);
    }
    arena_destroy(arena);
    puts("Finished!\n");
    printf("sizeof(unsigned long): %lu\n", sizeof(unsigned long));
    printf("sizeof(struct memblock): %lu\n", sizeof(struct memblock));
    return EXIT_SUCCESS;
}

